<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css/elementUI.css">
    <script src="../js/vue.js"></script>
    <script src="../js/axios.js"></script>
    <script src="../js/elementUI.js"></script>

</head>
<body>

<div id="app">

    <el-input v-model="query.title" placeholder="请输入相册名称" clearable style="width: 200px;"></el-input>
    <el-button type="primary" @click="toAdd">添加</el-button>
    <el-button type="danger" @click="delAll">批量删除</el-button>
    <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="id"
                label="编号">
        </el-table-column>
        <el-table-column
                prop="title"
                label="相册名称">
        </el-table-column>

        <el-table-column
                label="封面">
            <template slot-scope="scope">
                <img :src="scope.row.image" alt="" width="100px">
            </template>
        </el-table-column>
        <el-table-column
                label="数量">
            <template slot-scope="scope">
                {{JSON.parse(scope.row.imageItems).length}}
            </template>
        </el-table-column>

        <el-table-column label="操作">
            <template slot-scope="scope"><el-button
                    size="mini"
                    @click="goAlbumList(scope.$index, scope.row)">查看</el-button>
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>

    </el-table>

    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="query.page"
            :page-sizes="[10, 20, 30,40]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>


    <el-dialog :title="mode.title+'相册'" :visible.sync="dialogFormVisible" top="10px" :before-close="beforeCloseDialog">
        <el-form :model="form">
            <el-form-item label="名称" :label-width="formLabelWidth">
                <el-input v-model="form.title" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="图片" :label-width="formLabelWidth">
                <el-upload
                        action="/upload/uploadOss.do"
                        list-type="picture-card"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemove"
                        :on-success="uploadSuccess"
                        :file-list="fileList"
                        :limit="1"
                >
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible" append-to-body>
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="addBrand">确 定</el-button>
        </div>
    </el-dialog>

</div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            mode:{
                title:"",
                url:""
            },
            fileList:[],
            formLabelWidth:"120px",
            query:{
                title:"",
                page:1,
                size:10
            },
            form:{
                image:"",
                title:"",
            },
            tableData:[],
            total:0,
            multipleSelection:[],
            dialogImageUrl: '',
            dialogVisible: false,
            dialogFormVisible:false
        },
        watch:{
            query:{
                deep:true,
                handler(){
                    this.initData()
                }
            }
        },
        methods: {
            goAlbumList(index,row){
                location.href = "album_list.html?id="+row.id
            },
            handleEdit(index, row) {
                this.mode = {
                    title:"修改",
                    url:"/album/update.do"
                }
                this.form = JSON.parse(JSON.stringify(row));//普通输入框的回显
                this.fileList = [{name: row.name, url: row.image}];
                this.dialogFormVisible = true;
            },
            delAll(){
                if(this.multipleSelection.length>0){
                    this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.delBrand(this.multipleSelection.map(m=>m.id).join(","));
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }else{
                    this.$message('请选择一条数据');
                }
            },
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.delBrand(row.id);
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            delBrand(ids){
                axios.get(`/album/deleteAll.do?ids=${ids}`).then(res=>{
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.initData()
                })

            },
            beforeCloseDialog(close){

                this.initFormData();
                close()//关闭弹窗的方法
            },
            addBrand(){
                axios.post(this.mode.url,this.form).then(res=>{
                    this.initFormData();
                    this.initData()
                })
            },
            toAdd(){
                this.mode = {
                    title:"添加",
                    url:"/album/add.do"
                }
                this.dialogFormVisible = true;
            },
            initFormData(){
                this.form = {
                    image:"",
                    title:""
                }
                this.fileList = [];
                this.dialogFormVisible = false;

            },
            initData(){
                axios.post(`/album/findPage.do?page=${this.query.page}&size=${this.query.size}`,this.query).then(res=>{
                    this.tableData = res.data.rows;
                    this.total = res.data.total;
                    if(this.tableData.length==0&&this.query.page>1){
                        this.query.page--
                    }
                })

            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.query.size = val
            },
            handleCurrentChange(val) {
                this.query.page = val
            },
            handleRemove(file, fileList) {
                this.form.image = "";
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            uploadSuccess(res){
                this.form.image = res;
            }
        },
        created() {

            this.initData();
        }
    })
</script>
</html>